#https://blog.csdn.net/subfate/article/details/554415823
#

import os
import re
import struct
import sys

OUI_FILE = "oui.txt"
TXT_FILE =  "oui_txt.txt"
BIN_FILE = "oui.bin"

def write_file():
	line = 0
	list = []
	try:
		f = open(OUI_FILE, 'r')
		while True:
			l  = f.readline()
			if l == '':
				break
			line += 1
			l = l.strip('\n')
			ret = re.findall(r"^[A-F0-9].[A-F0-9].[A-F0-9].+$", l)
			if  len(ret) != 0:
				mac = l[:6]
				mac_int = int(mac, 16)
				org = l[22:]
				org.strip()
				test = mac+" " + org
				list.append(test)

		list.sort()
		f.close()
	except:
		raise
	line = 0

	try:
		f1 = open(BIN_FILE, 'wb')
		f2 = open(TXT_FILE, "w")
		f1.write("0000")

		for i in range(0,len(list)):
			line += 1
			mac = list[i][:6]
			mac_int = int(mac, 16)
			org =list[i][7:]
			format = '%ds' % len(org)
			org_byte = struct.pack(format, str.encode(org))
			org_len = len(org)
			byte = struct.pack('i', mac_int) + struct.pack('b',org_len) + struct.pack(format,str,encode(org))
			f1.write(byte)
			f1.close()
			f2.close()
		except:
			raise

	if __name__ == '__main__':
		reload(sys)
		sys.setdefaultencoding('utf-8')
		write_file()